package net.laoyeye.yyblog.web.frontend;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import net.laoyeye.yyblog.common.DataGridResult;
import net.laoyeye.yyblog.common.YYBlogResult;
import net.laoyeye.yyblog.model.TPlan;
import net.laoyeye.yyblog.model.query.BaseQuery;
import net.laoyeye.yyblog.service.KeywordService;
import net.laoyeye.yyblog.service.TPlanService;

@Controller
@RequestMapping("/api/plan")
public class APIPlanController {
    @Autowired
    private TPlanService tPlanService;
    
    @GetMapping
    public String index() {
        return "management/plan";
    }

//    @GetMapping("/list")
//    @ResponseBody
//    public DataGridResult listKeyword(BaseQuery query) {
//        DataGridResult result = tPlanService.listPage(query);
//        return result;
//    }
  @GetMapping("/list")
  @ResponseBody
    public DataGridResult listKeyword(BaseQuery query,int userId) {
        DataGridResult result = tPlanService.listPage(query);
        List<TPlan> list = (List<TPlan>) result.getData();
        Iterator<TPlan> it=list.iterator();
        while(it.hasNext()) {
            TPlan m=it.next();
            if(m.getUserId()!=userId) {
                it.remove();
            }
        }
        return result;
    }
    
    @PostMapping("/add")
    @ResponseBody
    public YYBlogResult add(@RequestBody  TPlan plan) {
        tPlanService.insertSelective(plan);
        return YYBlogResult.ok();
    }
    
    @PostMapping("/delete")
    @ResponseBody
    public YYBlogResult delete(long id) {

         tPlanService.deleteByPrimaryKey((int) id);
         return YYBlogResult.ok();
    }
    
    @PostMapping("/edit/enable")
    @ResponseBody
    public YYBlogResult editEnable(TPlan plan) {
    	tPlanService.updateByPrimaryKeySelective(plan);
        return YYBlogResult.ok();
    }

    @PostMapping("/edit/plan")
    @ResponseBody
    public YYBlogResult edit(TPlan plan) {
    	tPlanService.updateByPrimaryKeySelective(plan);
        return YYBlogResult.ok();
    }

}
